﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StringHelper;

namespace SharpSphinx.Result
{
     public class Edge
    {

         protected double acousticScore;
         protected double lmScore;
         protected Node fromNode;
         protected Node toNode;


         


         protected Edge(Node fromNode, Node toNode,double acousticScore,
             double lmScore)
            
         {
             this.acousticScore = acousticScore;
             this.lmScore = lmScore;
             this.fromNode = fromNode;
             this.toNode = toNode; 


         }

         public override string ToString()
         {
             return "Edge(" + fromNode + "-->" + toNode + '[' + acousticScore + ',' + lmScore + "])";
         }

         /// <summary>
         /// Internal routine used when creating a Lattice from a .LAT file
         /// </summary>
         /// <param name="lattrice"></param>
         /// <param name="Tokens"></param>
         static void Load(Lattrice lattrice,StringTokenizer Tokens)
         {
             string from = Tokens.Next().ToString();
             string to = Tokens.Next().ToString();

             int score = int.Parse(Tokens.Next().ToString());

             Node fromNode = lattrice.getNode(from);
             if (fromNode == null)
             {
                 throw new Exception("Edge fromNode\""+from +"\" does not exist"); 

             }

             Node toNode = lattrice.getNode(to); 
             if (toNode == null)
             {
                 throw new Exception("Edge toNode\""+to + "\" does not exist"); 
             }

             lattrice.AddEdge(fromNode,toNode ,score ,0.0);
          }


         public double AcousticScore 
         {
             get {
                 return acousticScore;
             }
         }

         public double LMScore 
         {
             get {
                 return lmScore;
             }
         }

         public Node FromNode 
         {
             get {
             
                 return fromNode;
             }
         }

         public Node ToNode 
         {
             get {
                 return toNode;
             }
         }

         public void SetAcousticScore(double v)
         {
             acousticScore = v;
         }

         public void SetLMScore(double v)
         {
             lmScore = v;
         }

         public bool IsEquivalent(Edge other) {
         
             double diff = Math.Abs(acousticScore) *0.00001; 
             return ((Math.Abs(acousticScore - other.AcousticScore <= diff && 
                 lmScore == other.LMScore && (fromNode.IsEquivalent(other.FromNode))&& 
                       toNode.IsEquivalent(other.ToNode))));
         }
    }
}
